package com.projet.glb2r.DAO;

import java.util.ArrayList;
import java.util.List;

import com.projet.glb2r.Base_et_Provider.Provider;
import com.projet.glb2r.Base_et_Provider.TableClient;
import com.projet.glb2r.Objet.ObjetClient;

import com.projet.glb2r.Objet.ObjetProduit;
import com.projet.glb2r.Base_et_Provider.TableProduit;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;

public class DAOClient {
	private final String ROWID = "ROWID";
	String [] allColumns = new String[] { ROWID, TableClient.KEY_TELEPHONE,
			TableClient.KEY_NOMCLIENT, TableClient.KEY_PRENOMCLIENT,
			TableClient.KEY_ADRESSE, TableClient.KEY_MAIL};


	public void insert(ObjetClient client, ContentResolver contentresolver) {
		ContentValues values = new ContentValues();
		values.put(TableClient.KEY_NOMCLIENT, client.getNom());
		values.put(TableClient.KEY_PRENOMCLIENT, client.getPrenom());
		values.put(TableClient.KEY_ADRESSE, client.getAdresse());
		values.put(TableClient.KEY_TELEPHONE, client.getTelephone());
		values.put(TableClient.KEY_MAIL, client.getMail());
		contentresolver.insert(Provider.CONTENT_URI_CLIENT, values);
	}

	public void deleteClient(ObjetClient client, ContentResolver contentResolver) {
		int id = getIdClientAvecNom(client, contentResolver);
		System.out.println("Client deleted with nom: " + client.getNom()+", Prenom : "+client.getPrenom());
		contentResolver.delete(Provider.CONTENT_URI_CLIENT, ROWID
				+ " = " + id, null);
	}

	public ArrayList<ObjetClient> getAllClient(ContentResolver contentResolver){
		ArrayList<ObjetClient> listeClients = new ArrayList<ObjetClient>();

		Cursor cursor = contentResolver.query(Provider.CONTENT_URI_CLIENT, 
				allColumns, null, null, null);


		if(cursor.getCount()==0)
			return listeClients;
		cursor.moveToFirst();
		do{
			ObjetClient client = cursorToClient(cursor);
			listeClients.add(client);
		}
		while(cursor.moveToNext());

		cursor.close();
		return listeClients;
	}
	
	String [] allColumns0 = new String[] { ROWID, TableClient.KEY_TELEPHONE,
			TableClient.KEY_NOMCLIENT, TableClient.KEY_PRENOMCLIENT,
			TableClient.KEY_ADRESSE, TableClient.KEY_MAIL};

	private ObjetClient cursorToClient(Cursor cursor) {
		ObjetClient client = new ObjetClient();
		client.setMail(cursor.getString(5));
		client.setAdresse(cursor.getString(4));
		client.setPrenom(cursor.getString(3));
		client.setNom(cursor.getString(2));
		client.setTelephone(cursor.getString(1));
		client.setID(cursor.getInt(0));
		return client;
	}


	public ObjetClient getClient(ContentResolver contentResolver) {
		ObjetClient client = new ObjetClient();
		Cursor cursor = contentResolver.query(Provider.CONTENT_URI_CLIENT, allColumns, null, null, null);
		cursor.moveToFirst();
		client = cursorToClient(cursor);
		return client;
	}
	
	public ObjetClient getClient(ContentResolver contentResolver, String selection, String [] selectionArgs) {
		ObjetClient client = new ObjetClient();
		Cursor cursor = contentResolver.query(Provider.CONTENT_URI_CLIENT, allColumns, selection, selectionArgs, null);
		cursor.moveToFirst();

		client = cursorToClient(cursor);
		return client;
	}
	

	public int getIdClientAvecNom(ObjetClient client, ContentResolver contentResolver) {
		Cursor cursor = contentResolver.query(Provider.CONTENT_URI_CLIENT, new String[]{ROWID}, TableClient.KEY_NOMCLIENT+" = ? and "+TableClient.KEY_PRENOMCLIENT+" = ? and "+TableClient.KEY_TELEPHONE+" = ?", new String[]{client.getNom(), client.getPrenom(), client.getTelephone()}, null);
		int idclient = cursor.getInt(0);
		return idclient;
	}
	


}
